<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Class: ActiveBackground::Database</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />

    <script language="JavaScript" type="text/javascript">
    // <![CDATA[

        function toggleSource( id )
        {
          var elem
          var link

          if( document.getElementById )
          {
            elem = document.getElementById( id )
            link = document.getElementById( "l_" + id )
          }
          else if ( document.all )
          {
            elem = eval( "document.all." + id )
            link = eval( "document.all.l_" + id )
          }
          else
            return false;

          if( elem.style.display == "block" )
          {
            elem.style.display = "none"
            link.innerHTML = "show source"
          }
          else
          {
            elem.style.display = "block"
            link.innerHTML = "hide source"
          }
        }

        function openCode( url )
        {
          window.open( url, "SOURCE_CODE", "width=400,height=400,scrollbars=yes" )
        }
      // ]]>
    </script>

  </head>

  <body>
  <table width="100%" border='0' cellpadding='0' cellspacing='0' class='banner'><tr>
  <td class="file-title"><span class="file-title-prefix">Class</span><br />ActiveBackground::Database</td>
  <td align="right">
    <table cellspacing=0 cellpadding=2>
      <tr valign="top">
        <td>In:</td>
        <td>

<a href="../../files/vendor/plugins/active_background/tasks/__/lib/active_background/database_rb.html">vendor/plugins/active_background/tasks/../lib/active_background/database.rb</a>
        </td>
      </tr>
    <tr>
      <td>Parent:</td>
      <td>
Object
     </td>
   </tr>
         </table>
        </td>
        </tr>
      </table>
 <!-- banner header -->

  <div id="bodyContent">
      <div id="content">




  <div class="sectiontitle">Methods</div>
  <ul>
  <li><a href="#M000058">lock</a></li>
  <li><a href="#M000060">prepare</a></li>
  <li><a href="#M000057">setup</a></li>
  <li><a href="#M000061">terminate</a></li>
  <li><a href="#M000059">unlock</a></li>
  </ul>


<div class="sectiontitle">Included Modules</div>
<ul>
  <li>Singleton</li>
</ul>






<div class="sectiontitle">Public Instance methods</div>
<div class="method">
  <div class="title">
    <a name="M000058"></a><b>lock</b>()
  </div>
<div class="sourcecode">
  <p class="source-link">[ <a href="javascript:toggleSource('M000058_source')" id="l_M000058_source">show source</a> ]</p>

  <div id="M000058_source" class="dyn-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/active_background/tasks/../lib/active_background/database.rb, line 62</span>
62:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">lock</span>
63:       <span class="ruby-comment cmt"># TODO: need to be able to lock until database actions drop below certain point</span>
64:       <span class="ruby-comment cmt"># lets put the thing into queue</span>
65:       <span class="ruby-ivar">@db</span>.<span class="ruby-identifier">synchronize</span> <span class="ruby-keyword kw">do</span> <span class="ruby-ivar">@queue</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span> <span class="ruby-keyword kw">end</span>
66:       <span class="ruby-comment cmt"># wait till there are locks available</span>
67:       <span class="ruby-keyword kw">while</span> <span class="ruby-ivar">@maximum</span> <span class="ruby-operator">&lt;=</span> <span class="ruby-ivar">@locks</span>
68:         <span class="ruby-identifier">sleep</span> <span class="ruby-value">1</span>
69:         <span class="ruby-ivar">@logger</span>.<span class="ruby-identifier">info</span> <span class="ruby-node">&quot;Database: Waiting for db lock.... (Currently there are #{@locks} db actions running and #{@queue} in queue)&quot;</span>
70:       <span class="ruby-keyword kw">end</span>
71:       <span class="ruby-comment cmt"># some info</span>
72:       <span class="ruby-ivar">@logger</span>.<span class="ruby-identifier">info</span> <span class="ruby-node">&quot;Database: Currently there are #{@locks+1} db actions running and #{@queue-1} in queue.&quot;</span>
73:       <span class="ruby-comment cmt"># increase lock and decrease queue</span>
74:       <span class="ruby-ivar">@db</span>.<span class="ruby-identifier">synchronize</span> <span class="ruby-keyword kw">do</span> 
75:         <span class="ruby-ivar">@queue</span> <span class="ruby-operator">-=</span> <span class="ruby-value">1</span>
76:         <span class="ruby-ivar">@locks</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>
77:       <span class="ruby-keyword kw">end</span>
78:     <span class="ruby-keyword kw">end</span>
</pre>
  </div>
</div>
</div>
<div class="method">
  <div class="title">
    <a name="M000060"></a><b>prepare</b>()
  </div>
<div class="sourcecode">
  <p class="source-link">[ <a href="javascript:toggleSource('M000060_source')" id="l_M000060_source">show source</a> ]</p>

  <div id="M000060_source" class="dyn-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/active_background/tasks/../lib/active_background/database.rb, line 87</span>
87:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">prepare</span>
88:       <span class="ruby-ivar">@config</span> = <span class="ruby-constant">ActiveBackground</span><span class="ruby-operator">::</span><span class="ruby-constant">Configuration</span>.<span class="ruby-identifier">instance</span>
89:       <span class="ruby-keyword kw">case</span> <span class="ruby-ivar">@config</span>.<span class="ruby-identifier">database</span>.<span class="ruby-identifier">lib_type</span>
90:         <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:active_record</span>
91:           <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">connection</span>
92:           <span class="ruby-identifier">results</span> = <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">connection</span>.<span class="ruby-identifier">execute</span>(<span class="ruby-value str">&quot;SHOW PROCESSLIST;&quot;</span>)
93:           <span class="ruby-identifier">c</span>=<span class="ruby-value">0</span>
94:           <span class="ruby-identifier">results</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">connection</span><span class="ruby-operator">|</span>
95:             <span class="ruby-identifier">c</span><span class="ruby-operator">+=</span><span class="ruby-value">1</span>
96:           <span class="ruby-keyword kw">end</span>
97:           <span class="ruby-constant">ACTIVE_BACKGROUND_LOGGER</span>.<span class="ruby-identifier">debug</span>(<span class="ruby-node">&quot;Database: Currently running processes: #{c}&quot;</span>)
98:       <span class="ruby-keyword kw">end</span>
99:     <span class="ruby-keyword kw">end</span>
</pre>
  </div>
</div>
</div>
<div class="method">
  <div class="title">
    <a name="M000057"></a><b>setup</b>()
  </div>
<div class="sourcecode">
  <p class="source-link">[ <a href="javascript:toggleSource('M000057_source')" id="l_M000057_source">show source</a> ]</p>

  <div id="M000057_source" class="dyn-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/active_background/tasks/../lib/active_background/database.rb, line 42</span>
42:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">setup</span>
43:       <span class="ruby-ivar">@locks</span> = <span class="ruby-value">0</span>
44:       <span class="ruby-ivar">@maximum</span> = <span class="ruby-value">50</span>
45:       <span class="ruby-ivar">@queue</span> = <span class="ruby-value">0</span>
46:       <span class="ruby-ivar">@db</span> = <span class="ruby-constant">Mutex</span>.<span class="ruby-identifier">new</span>
47:       <span class="ruby-ivar">@setup</span> = <span class="ruby-keyword kw">false</span>
48:       
49:       <span class="ruby-keyword kw">unless</span> <span class="ruby-ivar">@setup</span>
50:         <span class="ruby-ivar">@config</span> = <span class="ruby-constant">ActiveBackground</span><span class="ruby-operator">::</span><span class="ruby-constant">Configuration</span>.<span class="ruby-identifier">instance</span>
51:         <span class="ruby-ivar">@logger</span> = <span class="ruby-ivar">@config</span>.<span class="ruby-identifier">logger</span>
52:         <span class="ruby-keyword kw">case</span> <span class="ruby-ivar">@config</span>.<span class="ruby-identifier">database</span>.<span class="ruby-identifier">lib_type</span>
53:           <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:active_record</span>
54:             <span class="ruby-comment cmt"># FIXME: need to get this to work outside of rails environment</span>
55:             <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">allow_concurrency</span> = <span class="ruby-keyword kw">true</span>
56:             <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">establish_connection</span>(<span class="ruby-ivar">@config</span>.<span class="ruby-identifier">database</span>.<span class="ruby-identifier">connect_hash</span>)
57:         <span class="ruby-keyword kw">end</span>
58:         <span class="ruby-ivar">@setup</span> = <span class="ruby-keyword kw">true</span>
59:       <span class="ruby-keyword kw">end</span>
60:     <span class="ruby-keyword kw">end</span>
</pre>
  </div>
</div>
</div>
<div class="method">
  <div class="title">
    <a name="M000061"></a><b>terminate</b>()
  </div>
<div class="sourcecode">
  <p class="source-link">[ <a href="javascript:toggleSource('M000061_source')" id="l_M000061_source">show source</a> ]</p>

  <div id="M000061_source" class="dyn-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/plugins/active_background/tasks/../lib/active_background/database.rb, line 101</span>
101:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">terminate</span>
102:       <span class="ruby-ivar">@config</span> = <span class="ruby-constant">ActiveBackground</span><span class="ruby-operator">::</span><span class="ruby-constant">Configuration</span>.<span class="ruby-identifier">instance</span>
103:       <span class="ruby-keyword kw">case</span> <span class="ruby-ivar">@config</span>.<span class="ruby-identifier">database</span>.<span class="ruby-identifier">lib_type</span>
104:         <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:active_record</span>
105:           <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>.<span class="ruby-identifier">connection</span>.<span class="ruby-identifier">disconnect!</span>
106:       <span class="ruby-keyword kw">end</span>
107:     <span class="ruby-keyword kw">end</span>
</pre>
  </div>
</div>
</div>
<div class="method">
  <div class="title">
    <a name="M000059"></a><b>unlock</b>()
  </div>
<div class="sourcecode">
  <p class="source-link">[ <a href="javascript:toggleSource('M000059_source')" id="l_M000059_source">show source</a> ]</p>

  <div id="M000059_source" class="dyn-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/active_background/tasks/../lib/active_background/database.rb, line 80</span>
80:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">unlock</span>
81:       <span class="ruby-comment cmt"># remove the lock</span>
82:       <span class="ruby-ivar">@db</span>.<span class="ruby-identifier">synchronize</span> <span class="ruby-keyword kw">do</span>
83:         <span class="ruby-ivar">@locks</span> <span class="ruby-operator">-=</span> <span class="ruby-value">1</span>
84:       <span class="ruby-keyword kw">end</span>
85:     <span class="ruby-keyword kw">end</span>
</pre>
  </div>
</div>
</div>
</div>

  </div>

    </body>
</html>